<?php
/**
 * Created by PhpStorm.
 * User: zhangyurong
 * Date: 2016/8/26
 * Time: 17:41
 */
namespace App\Http\Controllers\Common;

use App\Facades\Site;
use App\Models\Common\CollTown;
use App\Models\Common\CollArea;
use App\Facades\Site as CollAreaSite;
use App\Facades\Style;
use App\Http\Controllers\Common\CommonController;
use Illuminate\Http\Request;
use Cache;


class AreaController extends CommonController{
    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明:打印省分json//一维
     */
    public function province(){
        $areas = CollArea::getProvince();
        return response()->json($areas);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明:打印省分加快选json//一维
     */
    public function shortcut(){
        $areas = CollArea::getProvince();
        $webareacode = substr(Site::get('webareacode'), 0, 4) . '00';
        $city = CollArea::getCity($webareacode);
        $areas = array("province" => $areas, "city" => $city);
        return response()->json($areas);
    }

    /**
     * @param $cid
     * @return \Illuminate\Http\JsonResponse
     * 说明:打印地级市json//一维
     */
    public function area($cid){
        $areas = CollArea::getArea($cid);
        return response()->json($areas);
    }

    /**
     * @param $cid
     * @return \Illuminate\Http\JsonResponse
     * 说明:打印县市区json//一维
     */
    public function city($cid){
        $areas = CollArea::getCity($cid);
        return response()->json($areas);
    }

    /**
     * @param $cid
     * @return \Illuminate\Http\JsonResponse
     * 说明:打印地级市json//一维
     */
    public function two_area($cid){
        $provinceid = '';
        //市编码
        $areaid = substr(Site::get('webareacode'), 0, 4) . '00';
        if($cid == 'auto'){
            $cid = substr($areaid, 0, 2) . '0000';
            $provinceid = $cid;
        }
        $areas = CollArea::getArea($cid);
        if(!empty($provinceid)){
            return response()->json(['areas' => $areas, 'provincecid' => $provinceid, 'areaid' => $areaid, 'status' => 1]);
        }else{
            return response()->json(['areas' => $areas, 'status' => 0]);
        }
    }


    /**
     * @param $cid
     * @return \Illuminate\Http\JsonResponse
     * 说明:打印县市区json//一维
     */
    public function two_city($cid){
        //获取县级市
        $areas = CollArea::getCity($cid);
        $webareacode = Site::get('webareacode');
        $towns = [];
        if(substr($webareacode, -2, 2) != '00'){
            $towns['area'] = ['value' => $webareacode, 'text' => CollArea::getAreaName($webareacode)];
            $towns['town'] = CollTown::getTown($webareacode);
            return response()->json(['areas' => $areas, 'towns' => $towns, 'status' => 1]);
        }else{
            return response()->json(['areas' => $areas, 'status' => 0]);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明：打印城市二维json
     */
    public function provinceArea(){
        $areas = CollArea::getProvinceArea();
        return response()->json($areas);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明：打印城市三维json
     */
    public function complete(){
        $areas = CollArea::getComplete();
        return response()->json($areas);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明：打印当前城市县级市和省份
     */
    public function province_area(){
        if(Cache::has('province_area')){
            $value = Cache::get('province_area');
        }else{
            $jsstr = '';
            $arr = array();
            //获取当前快选县级市
            $arr['city'] = CollArea::getSiteCity();
            //获取当前省份
            $arr['province'] = CollArea::getProvince();
            //获取当前县级市(附带市)
            $cid = substr(CollAreaSite::get('webareacode'), 0, 4) . '00';
            $city = CollArea::getAreaName($cid);
            $arr['city_two'] = CollArea::getSiteCity();
            array_unshift($arr['city_two'], ['value' => $cid, 'text' => $city]);

            foreach($arr as $k => $v){
                $option = json_encode($v);
                if($option != '[]') $jsstr .= 'var option_' . $k . '=' . $option . ';';//把json转成字符串拼接万js代码
            }
            $value = $jsstr ? $jsstr : '';
            if($value) Cache::forever('province_area', $value);
        }
        return $value ? $value : "var error_option= '没有找到对应的选项!';";
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明：单个获取当前城市县级市
     */
    public function currCity($cid){
        $name = (strlen($cid) == '8') ? CollTown::getTownName($cid) : CollArea::getAreaName($cid);
        return $name;
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明：多级选项获取当前城市县级市
     */
    public function getAreaName($arr){
        $areaName = array();
        $arr = explode(',', $arr);
        if(count($arr)){
            foreach($arr as $v){
                $areaName[] = array('value' => $v, 'text' => $this->currCity($v));
            }
        }
        return response()->json($areaName);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明：获取县市区json例表(附带市)
     */

    public function cityTwo($cid){
        $areas = array();
        $city = CollArea::getAreaName($cid);
        $areas = CollArea::getCity($cid);
        array_unshift($areas, ['value' => $cid, 'text' => $city]);
        return response()->json($areas);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明：多级选项获取当前城市县级市乡镇
     */
    public function getCityName($arr){
        $areaName = array();
        $arr = explode(',', $arr);
        if(count($arr)){
            foreach($arr as $v){
                $name = (strlen($v) == '8') ? CollTown::getTownName($v) : $this->currCity($v);
                $areaName[] = array('value' => $v, 'text' => $name);
            }
        }
        return response()->json($areaName);
    }


}